package com.jianqiao.delivery.expresscity.service.impl;

import com.jianqiao.delivery.delivery.mapper.ExpressPriceMapper;
import com.jianqiao.delivery.expresscity.entity.CityEntity;
import com.jianqiao.delivery.expresscity.entity.DistrictEntity;
import com.jianqiao.delivery.expresscity.entity.ProvinceEntity;
import com.jianqiao.delivery.expresscity.entity.StreetEntity;
import com.jianqiao.delivery.expresscity.mapper.CityMapper;
import com.jianqiao.delivery.expresscity.mapper.DistrictMapper;
import com.jianqiao.delivery.expresscity.mapper.ProvinceMapper;
import com.jianqiao.delivery.expresscity.mapper.StreetMapper;
import com.jianqiao.delivery.expresscity.service.ExpressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * author: cyx
 * create: 2024/8/30 10:45
 **/
@Service
public class ExpressServiceImpl implements ExpressService {

    @Autowired
    public ProvinceMapper provinceMapper;

    @Autowired
    public CityMapper cityMapper;

    @Autowired
    public DistrictMapper districtMapper;

    @Autowired
    public StreetMapper streetMapper;

    @Autowired
    public ExpressPriceMapper expressPriceMapper;


    @Override
    public void insertTable() {
        List<String> provinceList = expressPriceMapper.selectProvince();
        List<CityEntity> cityList = expressPriceMapper.selectCity();
        List<DistrictEntity> districtList = expressPriceMapper.selectDistrict();
        List<StreetEntity> streetList = expressPriceMapper.selectStreet();

        List<ProvinceEntity> provinceInsertList = new ArrayList<>();
        List<CityEntity> cityInsertList = new ArrayList<>();
        List<DistrictEntity> districtInsertList = new ArrayList<>();
        List<StreetEntity> streetInsertList = new ArrayList<>();
        for (int i = 0 ;i < provinceList.size() ;i ++){
            ProvinceEntity provinceEntity = new ProvinceEntity();
            provinceEntity.setProvinceId(i + 1);
            provinceEntity.setProvinceName(provinceList.get(i));
            provinceInsertList.add(provinceEntity);
        }
        System.out.println("provinceInsertList长度：" + provinceInsertList.size());
        int provinceCount = 0;
//        for (ProvinceEntity provinceEntity : provinceInsertList){
//            provinceMapper.insert(provinceEntity);
//            provinceCount ++;
//        }
        System.out.println("provinceCount ：" + provinceCount);

        for (int i = 0 ;i < cityList.size() ;i ++){
            CityEntity cityEntity = new CityEntity();
            cityEntity.setCityId(i + 1);
            cityEntity.setCityName(cityList.get(i).getCityName());
            cityEntity.setProvinceId(cityList.get(i).getProvinceId());
            cityInsertList.add(cityEntity);
        }
        System.out.println("cityInsertList长度：" + cityInsertList.size());
        int cityCount = 0;
//        for (CityEntity cityEntity : cityInsertList){
//            cityMapper.insert(cityEntity);
//            cityCount ++;
//        }
        System.out.println("cityCount ：" + cityCount);

        for (int i = 0 ;i < districtList.size() ;i ++){
            DistrictEntity districtEntity = new DistrictEntity();
            districtEntity.setDistrictId(i + 1);
            districtEntity.setDistrictName(districtList.get(i).getDistrictName());
            districtEntity.setCityId(districtList.get(i).getCityId());
            districtInsertList.add(districtEntity);
        }
        System.out.println("districtInsertList长度：" + districtInsertList.size());
        int districtCount = 0;
//        for (DistrictEntity districtEntity : districtInsertList){
//            districtMapper.insert(districtEntity);
//            districtCount ++;
//        }
        System.out.println("districtCount ：" + districtCount);

        for (int i = 0 ;i < streetList.size() ;i ++){
            StreetEntity streetEntity = new StreetEntity();
            streetEntity.setStreetId(i + 1);
            streetEntity.setStreetName(streetList.get(i).getStreetName());
            streetEntity.setDistrictId(streetList.get(i).getDistrictId());
            streetInsertList.add(streetEntity);
        }
        System.out.println("streetInsertList长度：" + streetInsertList.size());
        int streetCount = 0;
        for (StreetEntity streetEntity : streetInsertList){
            streetMapper.insert(streetEntity);
            streetCount ++;
        }
        System.out.println("streetCount ：" + streetCount);
    }
}
